//
//  ROLogonViewController.swift
//  NeuFood
//
//  Created by yangjingxin on 2017/10/12.
//  Copyright © 2017年 yangjingxin. All rights reserved.
//

import UIKit

class ROLogonViewController: UIViewController {

    @IBOutlet weak var userName: UITextField!
    
    @IBOutlet weak var passWord: UITextField!
    
    @IBAction func logon(_ sender: UIButton) {
    

    if userName.text == ""{
    //print("用户名没有输入")
    MBProgressHUD.showDelayHUDToView(self.view, message: "用户名没有输入")
    
    }
    else{
    if passWord.text == ""{
    //print("密码没有输入")
    MBProgressHUD.showDelayHUDToView(self.view, message: "没有密码输入")
    }
    else{
    //进行登录判断
    let RO_Logon: String = "http://60.205.189.39/userLogin.do"
    let paramenters: NSDictionary = ["username":userName.text!,"userpass":passWord.text!]as NSDictionary
    let afManager = AFHTTPRequestOperationManager()
    afManager.get(RO_Logon, parameters: paramenters, success: { (operation: AFHTTPRequestOperation?, responseObject:Any?) in
    
    //print(responseObject)
    let success = (responseObject as! NSDictionary).object(forKey: "userid")
    as! NSString
    if success != "0"{
    //print("登录成功")
    MBProgressHUD.showDelayHUDToView(self.view, message: "登录成功")
    
    let _=(ACSimpleKeychain.defaultKeychain() as AnyObject).storeUsername(self.userName.text, password: self.passWord.text, identifier: "user1", forService: "userpassword")
    }
    else{
    //print("登录失败")
    MBProgressHUD.showDelayHUDToView(self.view, message: "登录失败")
    
    }
    
    }, failure: { (operation:AFHTTPRequestOperation?, error:Error?) in
    
    })
    }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    
   // featchUserNamePwd()
    

}

func featchUserNamePwd(){
    
    let userPwdDict = (ACSimpleKeychain.defaultKeychain()
        as AnyObject).credentials(forIdentifier: "user1", service: "userpassword") as NSDictionary
    
    if userPwdDict.count != 0 {
        userName.text = userPwdDict.object(forKey: "username")
            as? String
        passWord.text = userPwdDict["password"] as? String
    }
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
